//
//  ViewController.m
//  JingXuanTableView
//
//  Created by TianZhen on 16/4/6.
//  Copyright © 2016年 TianZhen. All rights reserved.
//
#import "JingxuanModel.h"
#import "JingxuanTableView.h"
#import "JingxuanViewController.h"
#import "JingxuanCell.h"

#import "LoopView.h"

#define cellId @"JingxuanCellTz"
@interface JingxuanViewController ()<UITableViewDataSource,UITableViewDelegate>

/** 数据 */
@property (nonatomic,strong) NSArray *JingxuanModels;

@end

@implementation JingxuanViewController

#pragma mark - ======== view did load ========
- (void)loadView
{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建 tableView
    JingxuanTableView *tableView = [[JingxuanTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    NSArray *imageURL =
    @[@"http://image1.xunbaozl.com/FpacqN0P8yZENcp5-MdOYsK_imTX",
      @"http://image1.xunbaozl.com/FtS_1xFbKHVpbgmoGJcdgi0v5CX0",
      @"http://image1.xunbaozl.com/FkyXZZ6xW5Sqp1c_3wYNoAVpOKyk",
      @"http://image1.xunbaozl.com/FmLEQ4cAs4Ng_bzNSNNeXLor7Nja"];
    // 创建图片轮播器
    LoopView *lpView = [LoopView LoopViewWithURLs:imageURL titles:nil didSelected:^(NSInteger itemIndex) {
    }];
    lpView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 226);

    tableView.tableHeaderView = lpView;
    
    // 设置代理
    tableView.delegate = self;
    tableView.dataSource = self;
    
    // 注册cell
    UINib *nib = [UINib nibWithNibName:@"JingxuanCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:cellId];
    UINib *nib2 = [UINib nibWithNibName:@"XBNewCell" bundle:nil];
    [tableView registerNib:nib2 forCellReuseIdentifier:@"XBNewCell"];
    
    // 添加
    [self.view addSubview:tableView];
    
    // 其他设置
//    tableView.contentInset = UIEdgeInsetsMake(10, 0, 120, 0);
//    tableView.contentOffset = CGPointMake(0, -20);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
#pragma mark - ======== 数据源 ========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.JingxuanModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 获取当前模型
    JingxuanModel *model = self.JingxuanModels[indexPath.row];
    
    // 拿到cell
    JingxuanCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    // 赋值
    cell.model = model;
    
    return cell;
}

#pragma mark - ======== 代理 ========
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JingxuanCell *cell = [[NSBundle mainBundle] loadNibNamed:@"JingxuanCell" owner:nil options:nil].lastObject;

    return cell.frame.size.height;
}

#pragma mark - ======== 懒加载 ========
- (NSArray *)JingxuanModels
{
    if (!_JingxuanModels) {
        _JingxuanModels = [JingxuanModel models];
    }
    return _JingxuanModels;
}
@end
